<template>
  <div class="addresslist">
    <div class="top">
      <van-nav-bar
        title="地址管理"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
      />
    </div>
    <div class="content">
      <Bg>
        <van-address-list
          :list="addList"
          default-tag-text="默认"
          :switchable="false"
          @add="onAdd"
          @edit="onEdit"
        />
      </Bg>
    </div>
  </div>
</template>

<script>
import "../assets/less/addresslist.less";
import Bg from "../components/Bg.vue";
export default {
  name: "AddressList",
  components: {
    Bg,
  },
  data() {
    return {
      // 地址详情
      addList: [],
    };
  },
  created() {
    this.inquireAdd();
  },
  methods: {
    //新增地址
    onAdd() {
      this.$router.push({ name: "Address" });
    },

    // 编辑地址
    onEdit({ aid }) {
      this.$router.push({ name: "Address", params: { aid } });
    },

    // 查询地址
    inquireAdd() {
      let tokenString = this.$cookies.get("tokenString");

      if (!tokenString) {
        return this.$router.push({ name: "Login" });
      }

      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });

      this.axios({
        method: "get",
        url: this.baseUrl + "/findAddress",
        params: {
          appkey: this.appkey,
          tokenString: tokenString,
        },
      })
        .then((result) => {
          console.log(result);
          this.$toast.clear();
          if (result.data.code == 700) {
            return;
          }
          if (result.data.code === 20000) {
            result.data.result.forEach((v) => {
              v.address = `${v.province}${v.city}${v.county}${v.addressDetail}`;
              v.isDefault = !!v.isDefault;
              this.addList.push(v);
            });
          } else {
            this.$toast({
              message: result.data.msg,
              forbidClick: true,
              duration: 3000,
            });
          }
        })
        .catch((err) => {
          this.$toast.clear();
          console.log(err);
        });
    },

    // 返回
    onClickLeft() {
      this.$router.go(-1);
      // this.$router.push({ name: "" });
    },
  },
};
</script>
